﻿package com.firewolf.shiro.realm.realms;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;
/**
 * @Description: 定义的Realm
 * @Author: 刘兴
 * @Date : 2016年7月20日
 * @package : com.firewolf.shiro.realm
 * @Java_Version : 1.7
 * @Version : 2016年7月20日上午11:58:31
 */
public class MyRealm2 implements Realm{

	@Override
	public String getName() {
		return "myrealm2";
	}

	@Override
	public boolean supports(AuthenticationToken token) {
		//这里只通过用户名密码认证
		return token instanceof UsernamePasswordToken;
	}

	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		
		String name = (String) token.getPrincipal();
		String pwd = new String((char[]) token.getCredentials());
		if(!"wang".equals(name)){
			throw new UnknownAccountException("用户名不正确！");//用户名错误
		}
		if(!"123".equals(pwd)){
			throw new IncorrectCredentialsException("密码错误！");//密码错误
		}
		return new SimpleAuthenticationInfo(name+"@qq.com",pwd,getName());
	}

}
